var basePath = $("#basePath").val();
$(document).ready(function (){
    menuCss("厂商");
    var url = basePath + "/base/queryProvince";
    publicAjax(url,"POST",{},function (data) {
        if(data.ret == 200){
            initQueryManuData(data.data);
        }else {
            flavrShowByTime(data.msg,null,"danger",false);
            return;
        }
    });

    $("#queryForm").find("[name='cityId']").val(null);
    $("#queryForm").find("[name='cityId']").select2({
        language: 'zh-CN',
        placeholder: '请选择城市',
        allowClear: true});
    $("#queryForm").find("[name='areaId']").val(null);
    $("#queryForm").find("[name='areaId']").select2({
        language: 'zh-CN',
        placeholder: '请选择区域',
        allowClear: true});

    queryManuList(0,10);
    $("#show-count").change(function(){
        queryManuList(0,$("#show-count").val());
    })
    bindEvent();

    $('input[type="radio"].flat-green').iCheck({
        checkboxClass: 'icheckbox_flat-green',
        radioClass: 'iradio_flat-green'
    });
});


function initQueryManuData(object){
    var province = $("#queryForm").find("[name='provinceId']");
    $(province).html("");
    var html = "";
    $.each(object,function (index,item) {
        html += "<option value='"+item.provinceId+"'>"+item.province+"</option>"
    });
    $(province).html(html);
    $(province).val(null);
    $(province).select2({language: 'zh-CN',placeholder: "请选择省份",allowClear: true});
}



function bindEvent() {
    $("#search").click(function (){
        queryManuList(0,10)
    });

    $("#add").click(function (){
        initAddManuData();
        $("#add-modal").iziModal("open",this);
    });

    $("#saveManu").click(function (){
        saveManu();
    });

    $("#edit").click(function (){
        initAddManuData();
        editManuInitData();
    });

    $("#import").click(function (){
        location.href=basePath+"/manufacturer/import";
    });


    $("#bindMaintainer").click(function (){
        chekcBindMaintainerInit();
    });

    $("#bindSupplier").click(function (){
        checkBindSupplierInit();
    });


    $("#set-maintainer").find("[name='search']").click(function (){
        bindMaintainerInit();
    });

    $("#set-maintainer").find("[name='select']").click(function (){
        checkSetMaintainer();
    });

    $("#set-supplier").find("[name='select']").click(function (){
        checkSetSupplier();
    });

    $("#queryForm").find("[name='provinceId']").change(function (){
        var value = $(this).val();
        if(value == null || value == ""){
            $("#queryForm").find("[name='cityId']").html("");
            $("#queryForm").find("[name='areaId']").html("");
            return;
        }
        var url = basePath + "/base/queryCity";
        publicAjax(url,"POST",{"provinceId":value},function (data) {
            var city = $("#queryForm").find("[name='cityId']");
            $(city).html("");
            var html = "";
            $.each(data.data,function (index,item) {
                html += "<option value='"+item.cityId+"'>"+item.city+"</option>"
            });
            $(city).html(html);
            $(city).val(null);
            $(city).select2({language: 'zh-CN',placeholder: "请选择城市",allowClear: true});
        })
    });

    $("#queryForm").find("[name='cityId']").change(function (){
        var value = $(this).val();
        if(value == null || value == ""){
            $("#queryForm").find("[name='areaId']").html("");
            return;
        }
        var url = basePath + "/base/queryArea";
        publicAjax(url,"POST",{"cityId":value},function (data) {
            var area = $("#queryForm").find("[name='areaId']");
            $(area).html("");
            var html = "";
            $.each(data.data,function (index,item) {
                html += "<option value='"+item.areaId+"'>"+item.area+"</option>"
            });
            $(area).html(html);
            $(area).val(null);
            $(area).select2({language: 'zh-CN',placeholder: "请选择区域",allowClear: true});
        })

    });
}

function queryManuList(start,limit){
    var json = {};
    json.start = start;
    json.end = $("#show-count").val();
    var manufacturerName = $("#queryForm").find("[name='manufacturerName']").val();
    var provinceId = $("#queryForm").find("[name='provinceId']").val();
    var cityId = $("#queryForm").find("[name='cityId']").val();
    var areaId = $("#queryForm").find("[name='areaId']").val();
    json.manufacturerName = manufacturerName;
    json.provinceId = provinceId;
    json.cityId = cityId;
    json.areaId = areaId;
    var url = basePath + "/manufacturer/queryManufacturerList";
    publicAjax(url,"POST",json,function (data) {
        if(data.ret == 200){
            bindManuList(data.data.list);
            $("#total-count").text(data.data.count);
        }else{
            flavrShowByTime(data.msg,null,"danger",false);
        }
    });
}

function bindManuList(obj){
        $("#manu_table").html("");
        var html = "";
        if(obj == null || obj.length == 0){
            html="<tr><td class='text-center' colspan='12'>暂无数据</td></tr>";
        }

        $.each(obj,function (index,item) {
            html+="<tr><td class='text-center text-muted'>"+(index+1)+"</td>" +
                "<td class='text-center text-muted'><input type='checkbox' class='flat-green' name='one' value='"+item.manufacturerId+"'></td>" +
                "<td class='text-muted'>"+(item.manufacturerNo==null?'':item.manufacturerNo)+"</td>" +
                "<td class='text-muted'>"+(item.manufacturerName==null?'':item.manufacturerName)+"</td>" +
                "<td class='text-muted'>"+(item.shortName==null?'':item.shortName)+"</td>" +
                "<td class='text-muted'>"+(item.creditCode==null?'':item.creditCode)+"</td>" +
                "<td class='text-muted'>"+(item.registrationNumber==null?'':item.registrationNumber)+"</td>" +
                "<td class='text-muted'>"+(item.region==null?'':item.region)+"</td>" +
                "<td class='text-muted'><a href='javascript:showSupplier("+item.manufacturerId+")'>"+(item.supplierCount==null?0:item.supplierCount)+"</a></td>" +
                "<td class='text-muted'><a href='javascript:showMaintainer("+item.manufacturerId+")'>"+(item.maintainerCount==null?0:item.maintainerCount)+"</a></td>" +
                "<td class='text-muted'>"+(item.manufacturerContacts==null?'':item.manufacturerContacts)+"</td>" +
                "<td class='text-muted'>"+(item.manufacturerPhone==null?'':item.manufacturerPhone)+"</td>" +
                "<td class='text-muted'>"+(item.address==null?'':item.address)+"</td>" +
                "<td class='text-muted'>"+(item.serverPhone==null?'':item.serverPhone)+"</td>" +
                "</tr>"
        });
        $("#manu_table").html(html);
        initDomCss();
}

function showSupplier(id){
        var url = basePath + "/supplier/querySupplierListByManuId";
        $("#supplier_list").html();
        publicAjax(url,"POST",{supplierId:id},function(data){
            if(data.ret == 200){
                bindSupplierDate(data.data);
            }else{
                flavrShowByTime(data.msg,null,"danger",false);
            }
        });
}

function showMaintainer(id){
        var url = basePath + "/maintainer/queryMaintainerListByManuId";
        $("#maintainer_list").html("");
        publicAjax(url,"POST",{manufacturerId:id},function(data){
                if(data.ret == 200){
                    bindMaintainerDate(data.data);
                }else{
                    flavrShowByTime(data.msg,null,"danger",false);
                }
        });
}

function bindSupplierDate(obj){
    var html = "";
    if(obj == null || obj.length == 0){
        html="<tr><td class='text-muted text-center' colspan='8'>暂无维修商记录</td></tr>";
    }else{
        $.each(obj,function (index,item) {
            html+="<tr><td class='text-center text-muted'>"+(index+1)+"</td>" +
                "<td class='text-center text-muted'>"+(item.supplierNo==null?'':item.supplierNo)+"</td>" +
                "<td class='text-center text-muted'>"+(item.supplierName==null?'':item.supplierName)+"</td>" +
                "<td class='text-center text-muted'>"+(item.shortName==null?'':item.shortName)+"</td>" +
                "<td class='text-center text-muted'>"+(item.region==null?'':item.region)+"</td>" +
                "<td class='text-center text-muted'>"+(item.supplierContacts==null?'':item.supplierContacts)+"</td>" +
                "<td class='text-center text-muted'>"+(item.supplierPhone==null?'':item.supplierPhone)+"</td>" +
                "</tr>"
        });
    }
    $("#supplier_list").html(html);
    $("#supplier_modal").iziModal("open",this);
}

function bindMaintainerDate(obj){
        var html = "";
        if(obj == null || obj.length == 0){
            html="<tr><td class='text-muted text-center' colspan='8'>暂无维修商记录</td></tr>";
        }else{
            $.each(obj,function (index,item) {
                html+="<tr><td class='text-center text-muted'>"+(index+1)+"</td>" +
                    "<td class='text-center text-muted'>"+(item.maintainerNo==null?'':item.maintainerNo)+"</td>" +
                    "<td class='text-center text-muted'>"+(item.maintainerName==null?'':item.maintainerName)+"</td>" +
                    "<td class='text-center text-muted'>"+(item.shortName==null?'':item.shortName)+"</td>" +
                    "<td class='text-center text-muted'>"+(item.region==null?'':item.region)+"</td>" +
                    "<td class='text-center text-muted'>"+(item.maintainerContacts==null?'':item.maintainerContacts)+"</td>" +
                    "<td class='text-center text-muted'>"+(item.maintainerPhone==null?'':item.maintainerPhone)+"</td>" +
                    "<td class='text-center text-muted'>"+(item.level==null?'':item.level)+"</td>" +
                    "</tr>"
            });
        }
        $("#maintainer_list").html(html);
        $("#maintainer_modal").iziModal("open",this);
}




function initDomCss(){
    $("#manu_table").find('input[type="checkbox"].flat-green').iCheck({
        checkboxClass: 'icheckbox_flat-green',
        radioClass: 'iradio_flat-green'
    });

    $("#manu_table").find("input[name='one']").on('ifChecked', function(event){
        var current = $(this).parents("tr").index();
        $("[name='one']:checked").each(function(i){
            if($(this).parents("tr").index()!=current){
                $(this).iCheck('uncheck');
            }
        });
    });
}


$("#add-modal").iziModal({
    title: "编辑厂商",
    headerColor: '#00c0ef',
    rtl: false,
    width: 800,
    zindex: 1042,
    group: '',
    loop: false,
    history: false,
    restoreDefaultContent: false,
    autoOpen: false, // Boolean, Number
    bodyOverflow: false,
    fullscreen: true,
    openFullscreen: false,
    closeOnEscape: false,
    onOpening: function () {
        //initAddHospitalData();
        validator();
    },
    onOpened: function () {
    },
    onClosing: function () {
    },
    onClosed: function () {
        $("#add-update-from").find("[name='manufacturerId']").val(null);
        $('#add-update-from')[0].reset();
        $("#add-update-from").find("[name='cityId']").html("");
        $("#add-update-from").find("[name='areaId']").html("");
        $("#add-update-from").data('bootstrapValidator').destroy();

    }
});


$("#maintainer_modal").iziModal({
    title: "维修商信息",
    headerColor: '#00c0ef',
    rtl: false,
    width: 1000,
    zindex: 1042,
    group: '',
    loop: false,
    history: false,
    restoreDefaultContent: false,
    autoOpen: false, // Boolean, Number
    bodyOverflow: false,
    fullscreen: true,
    openFullscreen: false,
    closeOnEscape: false,
    onOpening: function () {
    },
    onOpened: function () {
    },
    onClosing: function () {
    },
    onClosed: function () {

    }
});

$("#supplier_modal").iziModal({
    title: "经销商信息",
    headerColor: '#00c0ef',
    rtl: false,
    width: 1000,
    zindex: 1042,
    group: '',
    loop: false,
    history: false,
    restoreDefaultContent: false,
    autoOpen: false, // Boolean, Number
    bodyOverflow: false,
    fullscreen: true,
    openFullscreen: false,
    closeOnEscape: false,
    onOpening: function () {
    },
    onOpened: function () {
    },
    onClosing: function () {
    },
    onClosed: function () {

    }
});


$("#set-maintainer").iziModal({
    title: "维修商列表",
    headerColor: '#00c0ef',
    rtl: false,
    width: 1000,
    zindex: 1042,
    group: '',
    loop: false,
    history: false,
    restoreDefaultContent: false,
    autoOpen: false, // Boolean, Number
    bodyOverflow: false,
    fullscreen: true,
    openFullscreen: false,
    closeOnEscape: false,
    onOpening: function () {
    },
    onOpened: function () {
    },
    onClosing: function () {
        $("#set-maintainer").find("[name='maintainerNo']").val("");
        $("#set-maintainer").find("[name='maintainerName']").val("");
        $("input[type='checkbox'][name='my']").iCheck("uncheck");
        $("input[type='checkbox'][name='total']").iCheck("uncheck");

    },
    onClosed: function () {

    }
});


$("#set-supplier").iziModal({
    title: "经销商列表",
    headerColor: '#00c0ef',
    rtl: false,
    width: 1000,
    zindex: 1042,
    group: '',
    loop: false,
    history: false,
    restoreDefaultContent: false,
    autoOpen: false, // Boolean, Number
    bodyOverflow: false,
    fullscreen: true,
    openFullscreen: false,
    closeOnEscape: false,
    onOpening: function () {
    },
    onOpened: function () {
    },
    onClosing: function () {
        $("#set-supplier").find("[name='supplierNo']").val("");
        $("#set-supplier").find("[name='supplierName']").val("");
        $("input[type='checkbox'][name='my1']").iCheck("uncheck");
        $("input[type='checkbox'][name='total1']").iCheck("uncheck");

    },
    onClosed: function () {

    }
});







function validator(){
    $('#add-update-from').bootstrapValidator({
        excluded: [':disabled', ':hidden', ':not(:visible)'],
        live: 'enabled',
        message: '非有效值',
        fields: {
            manufacturerName: {
                validators: {
                    notEmpty: {
                        message: '不能为空'
                    }
                }
            },manufacturerNo:{
                validators: {
                    notEmpty: {
                        message: '不能为空'
                    }
                }
            }
        }
    });
}


function initAddManuData(){
    $('input[type="radio"].flat-green').iCheck({
        checkboxClass: 'icheckbox_flat-green',
        radioClass: 'iradio_flat-green'
    });

    var province = $("#add-update-from").find("[name='provinceId']");
    var city = $("#add-update-from").find("[name='cityId']");
    var area = $("#add-update-from").find("[name='areaId']");

    $(province).html($("#queryForm").find("[name='provinceId']").html());
    $(province).val(null);

    $("#add-update-from").find("[name='provinceId']").select2({language: 'zh-CN',placeholder: "请选择",allowClear: true});
    $("#add-update-from").find("[name='cityId']").select2({language: 'zh-CN',placeholder: "请选择",allowClear: true});
    $("#add-update-from").find("[name='areaId']").select2({language: 'zh-CN',placeholder: "请选择",allowClear: true});


    $(province).change(function (){
        provinceChang(null,function (){
            $("#add-update-from").find("[name='cityId']").select2({language: 'zh-CN',placeholder: "请选择",allowClear: true});
        });
    });

    $(city).change(function (){
        cityChang(null,function (){
            $("#add-update-from").find("[name='areaId']").select2({language: 'zh-CN',placeholder: "请选择",allowClear: true});
        });
    });
}



function provinceChang(cityId,fn){
    var value = $("#add-update-from").find("[name='provinceId']").val();
    if(value == null || value == ""){
        $("#add-update-from").find("[name='cityId']").html("");
        $("#add-update-from").find("[name='areaId']").html("");
        return;
    }
    var url = basePath + "/base/queryCity";
    publicAjax(url,"POST",{"provinceId":value},function (data) {
        $("#add-update-from").find("[name='cityId']").html("");
        var html = "";
        $.each(data.data,function (index,item) {
            if(item.cityId == cityId){
                html += "<option value='"+item.cityId+"' selected>"+item.city+"</option>"
            }else{
                html += "<option value='"+item.cityId+"'>"+item.city+"</option>"
            }

        });
        $("#add-update-from").find("[name='cityId']").html(html);
        $("#add-update-from").find("[name='cityId']").val(null);
        fn();
    })

}

function cityChang(areaId,fn){
    var value = $("#add-update-from").find("[name='cityId']").val();
    if(value == null || value == ""){
        $("#add-update-from").find("[name='areaId']").html("");
        return;
    }
    var url = basePath + "/base/queryArea";
    publicAjax(url,"POST",{"cityId":value},function (data) {
        $("#add-update-from").find("[name='areaId']").html("");
        var html = "";
        $.each(data.data,function (index,item) {
            if(item.areaId == areaId){
                html += "<option value='"+item.areaId+"' selected>"+item.area+"</option>"
            }else{
                html += "<option value='"+item.areaId+"'>"+item.area+"</option>"
            }
        });
        $("#add-update-from").find("[name='areaId']").html(html);
        $("#add-update-from").find("[name='areaId']").val(null);
        fn();
    })
}

var bl = false;
function saveManu(){
    $('#add-update-from').bootstrapValidator('validate');
    if($('#add-update-from').data('bootstrapValidator').isValid()){
        if(!bl){
            bl = true;
            var url = basePath+"/manufacturer/addOrUpdateManufacturer"
            publicAjax(url,"POST",$("#add-update-from"),function (data) {
                if(data.ret == 200){
                    flavrShowByTime(data.msg,null,"success",false);
                    queryManuList(0,10);
                    $("#add-modal").iziModal("close", this);
                    bl=false;
                }else{
                    flavrShowByTime(data.msg,null,"danger",false);
                    bl=false;
                }
            })
        }
    }
}



function editManuInitData(){
    if(!$("input[name='one']:checked").length){
        flavrShowByTime("请选择一个厂商", null, "danger", false);
        return;
    }
    var json = {"manufacturerId":$("input[name='one']:checked").val()};
    var url=basePath+"/manufacturer/queryManufacturerInfo";
    publicAjax(url,"POST",json,function (data) {
        bindManuInfo(data.data);
    });
}
function bindManuInfo(obj){
        $("#add-update-from").find("[name='manufacturerId']").val(obj.manufacturerId);
        $("#add-update-from").find("[name='manufacturerName']").val(obj.manufacturerName);
        $("#add-update-from").find("[name='creditCode']").val(obj.creditCode);
        $("#add-update-from").find("[name='registrationNumber']").val(obj.registrationNumber);
        $("#add-update-from").find("[name='address']").val(obj.address);
        $("#add-update-from").find("[name='manufacturerContacts']").val(obj.manufacturerContacts);
        $("#add-update-from").find("[name='manufacturerPhone']").val(obj.manufacturerPhone);
        $("#add-update-from").find("[name='remark']").val(obj.remark);
        $("#add-update-from").find("[name='manufacturerNo']").val(obj.manufacturerNo);
        $("#add-update-from").find("[name='shortName']").val(obj.shortName);

    if(obj.provinceId != null){
        $("#add-update-from").find("[name='provinceId']").val(obj.provinceId);
        showText($("#add-update-from").find("[name='provinceId']"));
    }


    provinceChang(obj.cityId,function (){
        $("#add-update-from").find("[name='cityId']").val(obj.cityId);
        showText( $("#add-update-from").find("[name='cityId']"));

        cityChang(obj.areaId,function (){
            $("#add-update-from").find("[name='areaId']").val(obj.areaId).change();
            showText( $("#add-update-from").find("[name='areaId']"));
        });
    });

    $("input[type='radio'][name='isDel'][value='"+obj.isDel+"']").attr("checked",true);
    $("input[type='radio'][name='isDel'][value='"+obj.isDel+"']").addClass("checked");
    $('input[type="radio"].flat-green').iCheck({
        checkboxClass: 'icheckbox_flat-green',
        radioClass: 'iradio_flat-green'
    });
    $("#add-modal").iziModal("open",this);
}

function showText(obj){
    var text2 =$(obj).find("option:selected").text();
    $(obj).select2({language:'zh-CN', placeholder:text2, allowClear:true});
}


function checkBindSupplierInit(){
    if(!$("input[name='one']:checked").length){
        flavrShowByTime("请选择一个厂商", null, "danger", false);
        return;
    }else{
        bindSupplierInit();
    }
}

function bindSupplierInit(){
    var json = {"manufacturerId":$("input[name='one']:checked").val()};
    json.supplierNo = $("#set-maintainer").find("[name='supplierNo']").val();
    json.supplierName = $("#set-maintainer").find("[name='supplierName']").val();
    var url = basePath+"/manufacturer/bindSupplierInit";
    publicAjax(url,"POST",json,function (data){
        if(data.ret == 200){
            bindSupplierDatas(data.data);
        } else{
            flavrShowByTime(data.msg,null,"danger",false);
        }
    });
}

function bindSupplierDatas(obj){
    var html = "";
    if(obj == null || obj.length == 0){
        html="<tr><td class='text-muted text-center' colspan='8'>暂无维修商记录</td></tr>";
    }else{
        var bl = true;
        $.each(obj,function (index,item) {
            var ifchecked = "";
            if(item.flag == 1){
                ifchecked = "checked";
            }else{
                bl = false;
            }
            html+="<tr><td class='text-center text-muted'><input type='checkbox' name='my1' class='flat-green' "+ifchecked+" value='"+item.supplierId+"'></td>" +
                "<td class='text-center text-muted'>"+(item.supplierNo==null?'':item.supplierNo)+"</td>" +
                "<td class='text-center text-muted'>"+(item.supplierName==null?'':item.supplierName)+"</td>" +
                "<td class='text-center text-muted'>"+(item.shortName==null?'':item.shortName)+"</td>" +
                "<td class='text-center text-muted'>"+(item.region==null?'':item.region)+"</td>" +
                "<td class='text-center text-muted'>"+(item.supplierContacts==null?'':item.supplierContacts)+"</td>" +
                "<td class='text-center text-muted'>"+(item.supplierPhone==null?'':item.supplierPhone)+"</td>" +
                "</tr>"
        });
    }
    $("#set_supplier_list").html(html);

    $("#set-supplier").find('input[type="checkbox"].flat-green').iCheck({
        checkboxClass: 'icheckbox_flat-green',
        radioClass: 'iradio_flat-green'
    });
    if(bl){
        $("input[type='checkbox'][name='total1']").iCheck("check");
    }
    myCheckbox1();
    $("#set-supplier").iziModal("open",this);
}


function checkSetSupplier(){

    if(!$("input[name='my1']:checked").length){
        flavrShowByTime("请选择一个经销商", null, "danger", false);
        return;
    }
    saveSetSupplier();
}

function saveSetSupplier(){
    var supplierIds = "";
    $.each($("input[name='my1']:checked"),function (index,item){
        supplierIds+=$(item).val()+",";
    });

    if(supplierIds != ""){
        supplierIds = supplierIds.substring(0,supplierIds.length-1);
    }

    var url = basePath+"/manufacturer/saveSetSupplier";
    var json = {manufacturerId : $("input[name='one']:checked").val()};
    json.supplierIds = supplierIds;
    publicAjax(url,"POST",json,function (data) {
        if(data.ret == 200){
            flavrShowByTime(data.msg,null,"success",false);
            queryManuList(0,10)
            $("#set-supplier").iziModal("close",this);
        }else{
            flavrShowByTime(data.msg,null,"danger",false);
        }
    });
}


function chekcBindMaintainerInit(){
    if(!$("input[name='one']:checked").length){
        flavrShowByTime("请选择一个厂商", null, "danger", false);
        return;
    }else{
        bindMaintainerInit();
    }
}

function bindMaintainerInit(){
    var json = {"manufacturerId":$("input[name='one']:checked").val()};
    json.maintainerNo = $("#set-maintainer").find("[name='maintainerNo']").val();
    json.maintainerName = $("#set-maintainer").find("[name='maintainerName']").val();
    var url = basePath+"/manufacturer/bindMaintainerInit";
    publicAjax(url,"POST",json,function (data){
        if(data.ret == 200){
            bindMaintainerData(data.data);
        } else{
            flavrShowByTime(data.msg,null,"danger",false);
        }
    });
}


function bindMaintainerData(obj){
    var html = "";
    if(obj == null || obj.length == 0){
        html="<tr><td class='text-muted text-center' colspan='8'>暂无维修商记录</td></tr>";
    }else{
        var bl = true;
        $.each(obj,function (index,item) {
            var ifchecked = "";
            if(item.flag == 1){
                ifchecked = "checked";
            }else{
                bl = false;
            }
            html+="<tr><td class='text-center text-muted'><input type='checkbox' name='my' class='flat-green' "+ifchecked+" value='"+item.maintainerId+"'></td>" +
                "<td class='text-center text-muted'>"+(item.maintainerNo==null?'':item.maintainerNo)+"</td>" +
                "<td class='text-center text-muted'>"+(item.maintainerName==null?'':item.maintainerName)+"</td>" +
                "<td class='text-center text-muted'>"+(item.shortName==null?'':item.shortName)+"</td>" +
                "<td class='text-center text-muted'>"+(item.region==null?'':item.region)+"</td>" +
                "<td class='text-center text-muted'>"+(item.maintainerContacts==null?'':item.maintainerContacts)+"</td>" +
                "<td class='text-center text-muted'>"+(item.maintainerPhone==null?'':item.maintainerPhone)+"</td>" +
                "<td class='text-center text-muted'>"+(item.level==null?'':item.level)+"</td>" +
                "</tr>"
        });
    }
    $("#set_maintainer_list").html(html);

    $("#set-maintainer").find('input[type="checkbox"].flat-green').iCheck({
        checkboxClass: 'icheckbox_flat-green',
        radioClass: 'iradio_flat-green'
    });
    if(bl){
        $("input[type='checkbox'][name='total']").iCheck("check");
    }
    myCheckbox();
    $("#set-maintainer").iziModal("open",this);
}


function checkSetMaintainer(){
    if(!$("input[name='my']:checked").length){
        flavrShowByTime("请选择一个维修商", null, "danger", false);
        return;
    }
    saveSetMaintainer();
}

function saveSetMaintainer(){
    var maintainerIds = "";
    $.each($("input[name='my']:checked"),function (index,item){
        maintainerIds+=$(item).val()+",";
    });

    if(maintainerIds != ""){
        maintainerIds = maintainerIds.substring(0,maintainerIds.length-1);
    }

    var url = basePath+"/manufacturer/saveSetMaintainer";
    var json = {manufacturerId : $("input[name='one']:checked").val()};
    json.maintainerIds = maintainerIds;
    publicAjax(url,"POST",json,function (data) {
         if(data.ret == 200){
             flavrShowByTime(data.msg,null,"success",false);
             queryManuList(0,10)
             $("#set-maintainer").iziModal("close",this);
         }else{
             flavrShowByTime(data.msg,null,"danger",false);
         }
    });

}


function myCheckbox(){

    $("input[type='checkbox'][name='total']").on('ifClicked', function(event){
        $("input[type='checkbox'][name='my']").iCheck($(this).is(':checked')?'uncheck':'check');
    });
    $("input[type='checkbox'][name='my']").on('ifChecked', function(event){
        var change = true;
        $("input[type='checkbox'][name='my']").each(function(){
            if($(this).is(':checked')==false){
                change = false;
            }
        });
        if(change){
            $("input[type='checkbox'][name='total']").iCheck('check');
        }
    });

    $("input[type='checkbox'][name='my']").on('ifUnchecked', function(event){
        if($("input[type='checkbox'][name='total']").is(":checked")){
            $("input[type='checkbox'][name='total']").iCheck('uncheck');
        }
    });
}


function myCheckbox1(){

    $("input[type='checkbox'][name='total1']").on('ifClicked', function(event){
        $("input[type='checkbox'][name='my1']").iCheck($(this).is(':checked')?'uncheck':'check');
    });
    $("input[type='checkbox'][name='my1']").on('ifChecked', function(event){
        var change = true;
        $("input[type='checkbox'][name='my1']").each(function(){
            if($(this).is(':checked')==false){
                change = false;
            }
        });
        if(change){
            $("input[type='checkbox'][name='total1']").iCheck('check');
        }
    });

    $("input[type='checkbox'][name='my1']").on('ifUnchecked', function(event){
        if($("input[type='checkbox'][name='total1']").is(":checked")){
            $("input[type='checkbox'][name='total1']").iCheck('uncheck');
        }
    });
}